package gosu;

import java.nio.ByteBuffer;
import java.nio.FloatBuffer;

import org.jouvieje.FmodEx.Channel;
import static org.jouvieje.FmodEx.Misc.BufferUtils.*;

import static gosu.Gosu.FMOD;
import static gosu.Gosu.fmodCheck;
import static gosu.Gosu.relaxedFmodCheck;


public class SampleInstance {
  private Channel _channel;
  private float _freq;

  protected SampleInstance(Channel channel) {
    _channel = channel;

    // save the frequency for speed changing capability
    FloatBuffer buffer = newFloatBuffer(1);
    fmodCheck(_channel.getFrequency(buffer));
    _freq = buffer.get(0);
  }


  public void stop() {
    relaxedFmodCheck(_channel.stop());
  }


  public boolean isPlaying() {
    ByteBuffer buffer = newByteBuffer(1);
    relaxedFmodCheck(_channel.isPlaying(buffer));
    return buffer.get(0) != 0;
  }

  //TODO: Should these values be clamped?

  public void setPan(float pan) {
    relaxedFmodCheck(_channel.setPan(pan));
  }


  public void setVolume(float volume) {
    relaxedFmodCheck(_channel.setVolume(volume));
  }


  public void setSpeed(float speed) {
    relaxedFmodCheck(_channel.setFrequency(_freq * speed));
  }
}
